/* RadixSort.c - a single method which sorts arrays of items with keys that can be split into groups of bits - useful for unsigned integers or characters */ #include #include #include #include #include #include "Bins.h" #include "RadixSort.h" /* These parameters control the whole operation, they are set up once by a call to SetRadices They do not need to be re-set unless the radices change */ static int *n_bins; static unsigned int mask; static int *shifts; static int n_phases; int SetRadices( int n_bits, int n_radices ) { int i, tot_bits, bits_per_phase; /* Calculate how many bits in each phase */ bits_per_phase = n_bits/n_radices; if( (n_radices*bits_per_phase) < n_bits ) { bits_per_phase++; } /* Set up the mask */ mask = (1< n_bits ) { bits_per_phase = n_bits - tot_bits + bits_per_phase; } n_bins[i] = 1<>shifts[phase])&mask; } */ /* Implemented as a macro for speed! */ #define bin_number(x,phase) ((x>>shifts[phase])&mask) int *RSort( int *ip, int n ) { Bins b1, b2; int i, bn, phase; b2 = NULL; for(phase=0;phase